﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SuporeSliplampApp.Models;

namespace SuporeSliplampApp.Control
{
    public class PageEventArgs : EventArgs
    {
        public int Page { get; private set; } = 0;

        public PageEventArgs(int page)
        {
            Page = page;     
        }
    }
    /// <summary>
    /// Paginator.xaml 的交互逻辑
    /// </summary>
    public partial class Paginator : UserControl,INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged([CallerMemberName] string propertyName ="")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event Action<object, PageEventArgs> PageChanged;

        private int _page =1;

        public int Page
        {
            get { return _page; }
            set 
            { 
                _page = value;
                RaisePropertyChanged();
                Calc();
            }
        }

        private int _pages =1;

        public int Pages
        {
            get { return _pages; }
            set { _pages  = value; RaisePropertyChanged(); }
        }



        public int RecordCount
        {
            get { return (int)GetValue(RecordCountProperty); }
            set { SetValue(RecordCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RecordCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RecordCountProperty =
            DependencyProperty.Register("RecordCount", typeof(int), typeof(Paginator), 
                new PropertyMetadata(0, new PropertyChangedCallback(OnChangedCallback)));



        public int PageSize
        {
            get { return (int)GetValue(PageSizeProperty); }
            set { SetValue(PageSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PageSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register("PageSize", typeof(int), typeof(Paginator), 
                new PropertyMetadata(10,new PropertyChangedCallback(OnChangedCallback)));



        private static void OnChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Paginator paginator)
            { 
                paginator.Calc();
            }
        }

        private int _rightCount = 2;

        public int RightCount
        {
            get { return _rightCount; }
            set 
            {
                _rightCount = value;
                RaisePropertyChanged();
                Calc();
            }
        }

        private ObservableCollection<PageModel> _collection = new ObservableCollection<PageModel>();
        private List<PageModel> _list = null;


        public Paginator()
        {
            InitializeComponent();
            itemsControl.ItemsSource = _collection;
        }

        private void previousButton_Click(object sender, RoutedEventArgs e)
        {
            int previousPage = Page - 1;
            if(previousPage <1) previousPage = 1;
            if (previousPage != Page)
            {
                Page = previousPage;
                Calc();
                PageChanged?.Invoke(this, new PageEventArgs(previousPage));

            }
        }

        private void nextButton_Click(object sender, RoutedEventArgs e)
        {
            int nextPage = Page + 1;
            if(nextPage > Pages) nextPage = Pages;
            if (nextPage != Pages) 
            {
                Page = nextPage;
                Calc() ;
                PageChanged?.Invoke(this, new PageEventArgs(nextPage));
            }
        }

        /// <summary>
        /// 分页计算
        /// </summary>
        private void Calc()
        {
            Pages = (RecordCount - 1) / PageSize + 1;
            _list = new List<PageModel>();

            //第一页
            PageModel model = new PageModel(1, Page);
            _list.Add(model);

            //当前页码 连续页码
            for (int i= Page - RightCount; i<Page +RightCount; i++)
            {
                if (i > 0 && i < Pages)
                {
                    model = new PageModel(i, Page);
                    if (!_list.Exists(t => t.Page == model.Page))
                    { 
                        _list.Add(model);
                    }
                }
            }

            //最后页
            model = new PageModel(Pages, Page);
            if (! _list.Exists(t =>t.Page == model.Page))
            { 
                _list.Add(model);
            }

            for (int i= _list.Count-1; i>0; i--)
            {
                if (_list[i].Page - _list[i - 1].Page > 1)
                {
                    _list.Insert(i,new PageModel(0,Page,2));
                }
            }

            //按钮状态
            if (Page == 1)
            {
                previousButton.IsEnabled = false;
                previousButton.Foreground = new SolidColorBrush(Colors.AliceBlue);
            }
            else
            {
                previousButton.IsEnabled = true;
                previousButton.Foreground = new SolidColorBrush(Colors.White);
            }

            if (Page == Pages)
            {
                nextButton.IsEnabled = false;
                nextButton.Foreground = new SolidColorBrush(Colors.AliceBlue);
            }
            else
            {
                nextButton.IsEnabled = true;
                nextButton.Foreground = new SolidColorBrush(Colors.White);
            }

            _collection.Clear();
            foreach (var item in _list)
            {
                _collection.Add(item);
            }
        }

        private void numberButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton button = sender as RadioButton;
            PageModel model = button.CommandParameter as PageModel;
            if (model != null)
            {
                Page = model.Page;
                Calc();
                PageChanged?.Invoke(this, new PageEventArgs(model.Page));
            }
        }
    }
}
